package cn.wolfcode.wms.service.impl;

import cn.wolfcode.wms.domain.Employee;
import cn.wolfcode.wms.mapper.EmployeeMapper;
import cn.wolfcode.wms.query.EmployeeQueryObject;
import cn.wolfcode.wms.query.QueryObject;
import cn.wolfcode.wms.service.IEmployeeService;
import cn.wolfcode.wms.util.JSONResult;
import cn.wolfcode.wms.util.MD5;
import cn.wolfcode.wms.util.PageResult;
import cn.wolfcode.wms.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService{

    @Autowired
    private EmployeeMapper employeeMapper;

    public void saveOrUpdate(Employee entity,Long[] roleIds) {

        //判断是保存还是添加操作
        if (entity.getId() == null) {
            //保存操作中需要对密码加密
            String encoder = MD5.encoder(entity.getPassword());
            entity.setPassword(encoder);
            employeeMapper.insert(entity);
        }else{
            //修改操作中,首先要删除旧的关系,在重新保存上去
            employeeMapper.deleteByeId(entity.getId());
            employeeMapper.updateByPrimaryKey(entity);
        }

        //添加外键关系
        if (roleIds != null) {

            for (Long roleId : roleIds) {
                employeeMapper.insertByRelation(entity.getId(),roleId);
            }
        }
    }

   public void delete(Long id) {
        //先删除员工对应的角色关系之后在删除员工
       employeeMapper.deleteByeId(id);
        employeeMapper.deleteByPrimaryKey(id);
    }

    public Employee get(Long id) {

        return employeeMapper.selectByeIdRole(id);
    }


    public PageResult queryResult(EmployeeQueryObject qo) {
        Integer rows = employeeMapper.queryForCount(qo);
        //判断数据库中的记录数是否为null
        if (rows==0) {
            return PageResult.EMPRY_PAGE;
        }
        //===============
        //如果数据库中有数据
        List<Employee> data = employeeMapper.queryForPage(qo);

        return new PageResult(data,rows,qo.getCurrentPage(),qo.getPageSize());
    }

    public Employee userLoginCheck(String user, String password) {

        //需要对密码进行加密之后才能到数据库中查询,因为数据库中的密码是加密过的
        Employee emp = employeeMapper.userLoginCheck(user, MD5.encoder(password));
        if (emp == null) {//登陆失败
            //抛出异常
            throw new RuntimeException("账号密码不匹配,请重新登陆");
        }

        //登陆成功(把用户信息保存到Session中)
        UserContext.setCurrentUser(emp);

        //如果不是超级管理员,查询出用户的所有权限表达式
        if(!emp.isAdmin()){
            List<String> exps = employeeMapper.selectByEmployeeId(emp.getId());
            //把用户拥有的所有权限表达式存储到session中
            UserContext.setExpression(exps);
        }
        return emp;
    }

    public void deletes(Long[] ids) {
        List<Long> arr = new ArrayList<>();
        if (ids == null) {
            for (Long id : ids) {
                arr.add(id);
            }
        }
        employeeMapper.deletes(arr);
    }
}
